package com.cloudstreamservice8002.controller;

import com.cloudstreamservice8002.common.Result;
import com.cloudstreamservice8002.entity.RouteInfo;
import com.cloudstreamservice8002.service.RouteInfoService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/routeInfo")
public class RouteInfoController {
    @Resource
    private RouteInfoService routeInfoService;

    /**
     * 获取路线基本列表
     * 分页查询
     *
     * @return
     */
    @GetMapping("/list")
    public Result list(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, @RequestParam(value = "routeCode", required = false) String routeCode) {
        return Result.success(routeInfoService.getRouteInfo(pageNum, pageSize, routeCode));
    }

    /**
     * 创建/更新路线信息
     *
     * @param
     * @return
     */
    @PostMapping(value = {"/create", "/update"})
    public Result createOrUpdate(@RequestBody RouteInfo routeInfo) {
        if (routeInfo.getId() == null) {
            routeInfoService.create(routeInfo);
            return Result.success("创建成功");
        }
        Boolean updates = routeInfoService.updates(routeInfo);
        return Result.success("更新成功");
    }

    /**
     * 删除配送信息
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody RouteInfo routeInfo) {
        if (routeInfo.getId() != null) {
            boolean delete = routeInfoService.removeById(routeInfo.getId());
            return delete ? Result.success("删除成功") : Result.error("删除失败");
        }
        if (routeInfo.getRouteCode() != null) {
            boolean delete = routeInfoService.removeRouteCode(routeInfo.getRouteCode());
            return delete ? Result.success("删除成功") : Result.error("删除失败");
        }
        return Result.error("参数错误");
    }


}
